#!/bin/sh
echo $@
exit

bit_of_num()
{
    num=$1 
    count=1
    while [ true ]
    do
        let num=num/10
        if [ $num -ne 0 ];then
            let count++
        else
            break
        fi

    done
    echo $count
}

if [ -z $1 ] ;then
    max=20
else
    max=$1
fi

#$1,column
get_fmt()
{
    index=$1
    let real_num=index*index
    bit=$(bit_of_num $real_num)
    let bit++
    fmt="%${bit}d"
    echo $fmt
}

get_fmt_s()
{
    index=$1
    let real_num=index*index
    bit=$(bit_of_num $real_num)
    let bit++
    fmt="%${bit}s"
    echo $fmt
}

#print table header
for((index=0;index<$max;index++));
do
    printf "$(get_fmt $index)" $index
done

printf "\n"

#each row
for((i=1;i<$max;i++));
do
    printf "%2d" $i
    for((k=1;k<i;k++));
    do
        printf "$(get_fmt_s $k)" "."
    done
    for((c=i;c<$max;c++));
    do
        let value=$i*$c
        printf "$(get_fmt_s $c)" $value 
    done
    printf "\n"
done
